package com.web.expspringboot.mapper;


import com.web.expspringboot.entity.CartSt;
import com.web.expspringboot.entity.ProductSt;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CartMapper {

    // 查询购物车
    @Select("SELECT " +
            "user_id AS userId, " +
            "product_id AS productId, " +
            "updated_time AS updatedTime " +
            "FROM cart_st WHERE user_id = #{userId}")
    List<CartSt> selectCartByUserId(@Param("userId") long userId);

    // 添加购物车
    @Insert("INSERT INTO cart_st (user_id, product_id, updated_time) " +
            "VALUES (#{userId}, #{productId}, CURRENT_TIMESTAMP)")
    int insertCart(CartSt cart);

    // 删除购物车
    @Delete("DELETE FROM cart_st WHERE user_id = #{userId} AND product_id = #{productId}")
    int deleteCart(@Param("userId") long userId, @Param("productId") long productId);

    // 购买商品（从购物车中删除该商品）
    @Delete("DELETE FROM cart_st WHERE user_id = #{userId} AND product_id = #{productId}")
    int purchaseProduct(@Param("userId") long userId, @Param("productId") long productId);

    @Select("SELECT " +
            "p.product_id AS productId, " +
            "p.product_title AS productTitle, " +
            "p.product_intro AS productIntro, " +
            "p.product_price AS productPrice, " +
            "p.product_imgs AS productImgs, " +
            "p.publish_time AS publishTime, " +
            "p.owner_id AS ownerId, " +
            "p.location AS location, " +
            "p.status AS status " +
            "FROM product_st p " +
            "JOIN cart_st c ON p.product_id = c.product_id " +
            "WHERE c.user_id = #{userId}"
            + " ORDER BY c.updated_time DESC ")
    List<ProductSt> getCartProducts(Long userId);
}
